package ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Work.Book;
import Work.BookList;
import Work.ChangeBook;
import Work.ChangeLibrarian;
import Work.ChangeReader;
import Work.ForeignKey;
import Work.Jdbc;
import Work.Librarian;
import Work.Reader;

public class BorrowSystem extends JFrame implements ActionListener{
	JTextField jtf1,jtf2,jtf3;
	private JButton returnbutton;
	Jdbc db = new Jdbc();
	public void Borrowbutton() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(2, 2, 0, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("书籍借阅面板"));
		JLabel jl1 = new JLabel("书籍编号:");
		jtf1 = new JTextField(10);
		JLabel jl2 = new JLabel("读者编号:");
		jtf2 = new JTextField(10);
		
		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf1);// 在面板jp1上添加输入框
		jp1.add(jl2);
		jp1.add(jtf2);

		jpc.add(jp1);// 把面板jp1加到流式布局上

		add(jpc);// 添加流式布局

		JButton jb1 = new JButton("借阅");
		jb1.addActionListener(this);
		jb1.setActionCommand("yes1");// 没看懂
		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");
		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("书籍借阅界面");
		setSize(280, 155);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}
	
	public void Returnbutton() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(3, 2, 0, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("书籍归还面板"));
		JLabel jl1 = new JLabel("书籍编号:");
		jtf1 = new JTextField(10);
		JLabel jl2 = new JLabel("读者编号:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("管理员编号:");
		jtf3 = new JTextField(10);
		
		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf1);// 在面板jp1上添加输入框
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);

		jpc.add(jp1);// 把面板jp1加到流式布局上

		add(jpc);// 添加流式布局

		JButton jb1 = new JButton("归还");
		jb1.addActionListener(this);
		jb1.setActionCommand("yes2");// 没看懂
		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");
		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("书籍归还界面");
		setSize(280, 185);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}
	
	public void Reborrowbutton() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(2, 2, 0, 10));// 设置新面板
		jp1.setBorder(BorderFactory.createTitledBorder("书籍续借面板"));
		JLabel jl1 = new JLabel("书籍编号:");
		jtf1 = new JTextField(10);
		JLabel jl2 = new JLabel("读者编号:");
		jtf2 = new JTextField(10);
		
		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf1);// 在面板jp1上添加输入框
		jp1.add(jl2);
		jp1.add(jtf2);

		jpc.add(jp1);// 把面板jp1加到流式布局上

		add(jpc);// 添加流式布局

		JButton jb1 = new JButton("续借");
		jb1.addActionListener(this);
		jb1.setActionCommand("yes3");// 没看懂
		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");
		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("书籍续借界面");
		setSize(280, 155);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);

	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		BorrowSystem BorrowSystem = new BorrowSystem();
		ForeignKey foreignkey = new ForeignKey();
        ChangeLibrarian changelibrarian = new ChangeLibrarian();
        ArrayList<Librarian> librarianlist = changelibrarian.read(db);
        ChangeBook changebook = new ChangeBook();
        BookList bList = new BookList();
        ArrayList<Book> booklist = changebook.read(db);
        ChangeReader changereader = new ChangeReader();
        ArrayList<Reader> readerlist = changereader.read(db);
        SimpleDateFormat df = new SimpleDateFormat("yyyy"+"-"+"MM"+"-"+"dd");
        String now = df.format(System.currentTimeMillis());
		if (e.getActionCommand().equals("yes1")) {
			String text1 = jtf1.getText().trim();
			String text2 = jtf2.getText().trim();
			if (text1.equals("")||text2.equals("")) {
				JOptionPane.showMessageDialog(this, "你还没有输入完整内容");
			}else if (!foreignkey.FindB_ID(booklist, text1)) {
				JOptionPane.showMessageDialog(this, "查无此书，请重新输入！");
			}else if (!foreignkey.IfZero(booklist, text1) ) {
				JOptionPane.showMessageDialog(this, "该书库存不足，请换一本");
			}else if (!foreignkey.FindR_ID(readerlist, text2)) {
				JOptionPane.showMessageDialog(this, "读者编号输入错误，请重新输入！");
			} else {
				db.uiborrowsystem(db, text1, text2, "0", 1);
				JOptionPane.showMessageDialog(this, "书籍借阅成功！\n请注意借阅时间："+now+"\n请在一个月内归还书籍\n办理续借手续请在归还期限内办理");
				this.dispose();
				BorrowSystem.Borrowbutton();
			}
		} else if (e.getActionCommand().equals("yes2")) {
			String text1 = jtf1.getText().trim();
			String text2 = jtf2.getText().trim();
			String text3 = jtf3.getText().trim();
			if (text1.equals("")||text2.equals("")||text3.equals("")) {
				JOptionPane.showMessageDialog(this, "你还没有输入完整内容");
			}else if (!foreignkey.FindB_ID(booklist, text1) ) {
				JOptionPane.showMessageDialog(this, "查无此书，请重新输入！");
			}else if (!foreignkey.FindR_ID(readerlist, text2)) {
				JOptionPane.showMessageDialog(this, "读者编号输入错误，请重新输入！");
			}else if (!foreignkey.FindL_ID(librarianlist, text3)) {
				JOptionPane.showMessageDialog(this, "管理员编号输入错误，请重新输入！");
			} else {
				db.uiborrowsystem(db, text1, text2, text3, 2);
				JOptionPane.showMessageDialog(this, "书籍归还成功");
				this.dispose();
				BorrowSystem.Returnbutton();
			}
		} else if (e.getActionCommand().equals("yes3")) {
			String text1 = jtf1.getText().trim();
			String text2 = jtf2.getText().trim();
			if (text1.equals("")||text2.equals("")) {
				JOptionPane.showMessageDialog(this, "你还没有输入完整内容");
			}else if (!foreignkey.FindB_ID(booklist, text1) ) {
				JOptionPane.showMessageDialog(this, "查无此书，请重新输入！");
			}else if (!foreignkey.FindR_ID(readerlist, text2)) {
				JOptionPane.showMessageDialog(this, "读者编号输入错误，请重新输入！");
			} else {
				db.uiborrowsystem(db, text1, text2, "0", 3);
				JOptionPane.showMessageDialog(this, "书籍续借成功！\n请注意借阅时间："+now+"\n请在一个月内归还书籍");
				this.dispose();
				BorrowSystem.Reborrowbutton();
			}
		} else if (e.getActionCommand().equals("no")) {// 返回
			this.dispose();
			new BorrowSystemOp();
		} else {
			jtf1.setText("");
			jtf2.setText("");
			jtf3.setText("");
		}
	}
}
